import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import gnu.trove.map.hash.TLongLongHashMap;

public class Cm11Lookup {
    //private static Map<Long, Long> lkpTable;
  private static TLongLongHashMap lkpTbl;
    
    public static void main(String args[]) {
        System.out.println("Reading in Lookup Table. Max memory that can be allocated:"+((Runtime.getRuntime().maxMemory())/(1024*1024))+"M");
        
        System.out.println("At program start up - Total:"+((Runtime.getRuntime().totalMemory()/(1024*1024)))+"M "+
                                     "Free:"+((Runtime.getRuntime().freeMemory())/(1024*1024))+"M "+
                                     "Used:"+(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/(1024*1024)+"M ");
       // lkpTable = new HashMap<Long, Long>(95000000, 0.9f);
        
        /*System.out.println("After Hashmap init - Total:"+((Runtime.getRuntime().totalMemory()/(1024*1024)))+"M "+
                                     "Free:"+((Runtime.getRuntime().freeMemory())/(1024*1024))+"M "+
                                     "Used:"+(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/(1024*1024)+"M ");*/
        
        int count = 0;
        String line;
        File file = new File("/idn/IMPAC_DATA/work/lkup/distinct_cm11");
        BufferedReader br;
        long[] l1 = new long[70000000];
        long[] l2 = new long[70000000];
        try {
            br = new BufferedReader(new FileReader(file));
            br.readLine();
            br.readLine();
            while ((line = br.readLine()) != null) {
                //lkpTable.put(Long.parseLong(line.substring(0, 11)), Long.parseLong(line.substring(12, 23)));
                //lkpTable.put(Long.parseLong(line.substring(0, 11)),0L);
                l1[count] = Long.parseLong(line.substring(0, 11));
                l2[count] = Long.parseLong(line.substring(12, 23));
                count++;
                line = null;
                if(count % 1000000 == 0) {
                  System.out.println(count / 1000000);
                  System.out.println("Total:"+((Runtime.getRuntime().totalMemory()/(1024*1024)))+"M "+
                                     "Free:"+((Runtime.getRuntime().freeMemory())/(1024*1024))+"M "+
                                     "Used:"+(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/(1024*1024)+"M ");
                  System.out.println("----------------------------");
                }
            }
            lkpTbl = new TLongLongHashMap(l1,l2);
            System.out.println("Done....."+
                               "Total:"+((Runtime.getRuntime().totalMemory()/(1024*1024)))+"M "+
                               "Free:"+((Runtime.getRuntime().freeMemory())/(1024*1024))+"M "+
                               "Used:"+(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/(1024*1024)+"M ");
            //lookup few random keys to test performance
            System.out.println("Doing lookup:"+System.currentTimeMillis());
            long rslt = lkpTbl.get(l1[34567]);
            System.out.println(rslt+" - Doing lookup:"+System.currentTimeMillis());
            rslt = lkpTbl.get(l1[876543]);
            System.out.println(rslt+" - Doing lookup:"+System.currentTimeMillis());
            rslt=lkpTbl.get(l1[54327]);
            System.out.println(rslt+" - Doing lookup:"+System.currentTimeMillis());
                            
        } catch (Exception e) {
            System.out.println("Trouble reading Cm11 Lookup Table -- " + e);
        }
    }
}